package me.xiaoyan.ehr.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import me.xiaoyan.ehr.config.annotation.OperationLog;
import me.xiaoyan.ehr.po.Job;
import me.xiaoyan.ehr.service.JobService;
import me.xiaoyan.ehr.vo.ApiResponseData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/job")
@ApiOperation(
        value = "职位管理"
)
public class JobController {
    private JobService jobService;

    @Autowired
    public void setJobService(JobService jobService) {
        this.jobService = jobService;
    }

    @GetMapping("/list")
    @ApiOperation(
            value = "查询职位列表", httpMethod = "GET", notes = "查询职位列表"
    )
    @OperationLog("职位管理")
    public ApiResponseData<IPage<Job>> getList(@RequestParam Map<String, String> params) {
        IPage<Job> list = jobService.selectByParams(params);
        ApiResponseData<IPage<Job>> jobList = new ApiResponseData<>();
        jobList.setData(list);
        return jobList;
    }

    @PostMapping("/update")
    @ApiOperation(
            value = "更新职位信息", httpMethod = "POST", notes = "更新职位信息"
    )
    @ApiImplicitParams({
            @ApiImplicitParam(required = true, name = "job", value = "职位json对象", dataType = "Job")
    })
    public ApiResponseData update(@RequestBody Job job) {
        ApiResponseData data = new ApiResponseData();
        jobService.updateById(job);
        return data;
    }

    @PostMapping("/create")
    @ApiOperation(
            value = "新增职位信息", httpMethod = "POST", notes = "新增职位信息"
    )
    @ApiImplicitParams({
            @ApiImplicitParam(required = true, name = "job", value = "职位json对象", dataType = "Job")
    })
    public ApiResponseData create(@RequestBody Job job) {
        ApiResponseData data = new ApiResponseData();
        jobService.save(job);
        return data;
    }

    // 保存职位 当id为空时 则新增否则为保存
    @PostMapping("/save")
    @ApiOperation(
            value = "新增职位信息", httpMethod = "POST", notes = "新增职位信息"
    )
    @ApiImplicitParams({
            @ApiImplicitParam(required = true, name = "job", value = "职位json对象", dataType = "Job")
    })
    public ApiResponseData save(@RequestBody Job job) {
        ApiResponseData data = new ApiResponseData();
        if (job.getId() != null && job.getId() > 0) {
            jobService.updateById(job); // 更新
        } else {
            jobService.save(job); // 新增
        }
        return data;
    }
}
